import { Component, OnInit } from '@angular/core';
import { ParseObject } from "../../../parse/parse-object"

// 引入Parse第三方库
import * as Parse from "parse"
(Parse as any).serverURL = "http://metapunk.cn:9999/parse"
Parse.initialize("dev")

@Component({
  selector: 'app-page-student',
  templateUrl: './page-student.component.html',
  styleUrls: ['./page-student.component.scss']
})
export class PageStudentComponent implements OnInit {

  StudentListTable = [
    {team:"智购",studentid:"5220841",name:"张如俊",mobile:"17379676625",gitee:	"zhang-ru-jun"},
    {team:"智购",studentid:"5220719",name:"陈洪斌",mobile:"18172778126",gitee:	"18172778126"},
    {team:"智购",studentid:"5220492",name:"林桂全",mobile:"18770733901",gitee:	"lin-xingstar"},
  ] // temp

  async ngOnInit(): Promise<void> {
    // 通过ParseJSSDK实现增删查改
    // 创建Student
    let Student = Parse.Object.extend("Student")
    let xiaoming = new Student();
    xiaoming.set({name:"来自JSSDK的小明"})
    xiaoming.save();
    // 查询Student
    let query = new Parse.Query("Student")
    query.equalTo("age",18)
    query.limit(10)
    let slist = await query.find()
    console.log(slist)
    // 修改Student
    let query2 = new Parse.Query("Student")
    let stu1 = await query2.get("FFF6d5wBGq");
    stu1.set("age",19)
    stu1.save()
    // 删除Student
    let query3 = new Parse.Query("Student")
    let stu2 = await query3.get("0aXlM26KPJ");
    stu2.destroy()

    return
    // 通过面向对象Class实现的增删查改
    let Student2 = new ParseObject("Student")
    // 查询Student列表
    let StudentList = await Student.get()
    console.log(StudentList)


    return
    // 通过面向过程方式实现的Student增删查改
    let list = await this.getStudentList()
    this.StudentListTable = this.StudentListTable.concat(list)
    // 智购	5220841	张如俊	17379676625	zhang-ru-jun
    // 智购	5220719	陈洪斌	18172778126	18172778126
    // 智购	5220492	林桂全	18770733901	lin-xingstar
    /**
     * 创建单个数据
     */
    let student = {name:"张如俊",studentid:"5220841",team:"智购",mobile:"17379676625",gitee:"zhang-ru-jun"}
    this.createStudent(student)
    /**
     * 创建多条数据
     * @desc 从你前端项目做好的测试数据列表，批量创建测试数据
     */
    this.StudentListTable.forEach(student=>this.createStudent(student))

    /**
     * 根据Student.objectId，更新指定对象数据
     */
    this.updateStudentById("8CAGf4lJCv",{name:"gogogo666"})

    /**
     * 根据Student.objectId，删除指定对象数据
     */
    // 3TdcBGGT6T YVDqaDuVpo D6KwZ4LSYG
    this.deleteStudentById("YVDqaDuVpo")
  }
  async deleteStudentById(objectId:string){
    let result = await fetch(`http://metapunk.cn:9999/parse/classes/Student/${objectId}`, {
      "headers": {
        "accept": "*/*",
        "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
        "x-parse-application-id": "dev"
      },
      "referrer": "http://localhost:4040/",
      "referrerPolicy": "strict-origin-when-cross-origin",
      "body": null,
      "method": "DELETE",
      "mode": "cors",
      "credentials": "omit"
    });
    let data = await result.json()
    console.log(data)
  }
  async updateStudentById(objectId:string,student:any){
    let json = JSON.stringify(student)
    let result = await fetch(`http://metapunk.cn:9999/parse/classes/Student/${objectId}`, {
      "headers": {
        "accept": "*/*",
        "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
        "content-type": "text/plain;charset=UTF-8",
        "x-parse-application-id": "dev"
      },
      "referrer": "http://localhost:4040/",
      "referrerPolicy": "strict-origin-when-cross-origin",
      "body": json,
      "method": "PUT",
      "mode": "cors",
      "credentials": "omit"
    });
    let data = await result.json()
    console.log(data)
  }

  async getStudentByStudentId(studentid:string){
    let result = await fetch(`http://metapunk.cn:9999/parse/classes/Student?where={%22studentid%22:%22${studentid}%22}&limit=1`, {
    "headers": {
      "accept": "*/*",
      "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
      "x-parse-application-id": "dev"
    },
    "referrer": "http://localhost:4040/",
    "referrerPolicy": "strict-origin-when-cross-origin",
    "body": null,
    "method": "GET",
    "mode": "cors",
    "credentials": "omit"
  });
    let data:any = await result.json();
    let list = data?.results
    console.log(list)
    return list[0]
  }
  /**
   * 根据传入的Object数据，创建Student学生数据，返回创建的objectId
   * @param student 学生数据Object对象
   */
  async createStudent(student:any){

    /**
     * 先根据学号查重，再执行创建逻辑
     */
    if(student?.studentid){
      let exists = await this.getStudentByStudentId(student?.studentid)
      if(exists) return // 存在时，返回不执行创建
    }
    /**
     * JS语言环境的Object，要通过网络请求，转成JSON字符串，再发送
     * @see
     * JSON.striginfy object => json string
     * JSON.parse json string => object
     */
    let jsonString = JSON.stringify(student)
    let result = await fetch("http://metapunk.cn:9999/parse/classes/Student", {
      "headers": {
        "accept": "*/*",
        "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
        "content-type": "text/plain;charset=UTF-8",
        "x-parse-application-id": "dev"
      },
      "referrer": "http://localhost:4040/",
      "referrerPolicy": "strict-origin-when-cross-origin",
      "body": jsonString,
      "method": "POST",
      "mode": "cors",
      "credentials": "omit"
    });
    // JSON.parse('{"name":"张如俊","studentid":"5220841","team":"智购","mobile":"17379676625","gitee":"zhang-ru-jun"}')
    let data = await result.json();
    console.log(data)
  }
  async getStudentList(){
    let result = await fetch("http://metapunk.cn:9999/parse/classes/Student?", {
      "headers": {
        "accept": "*/*",
        "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
        "if-none-match": "W/\"164-Qy3vijCmC7iE0mND19p6WsFbmSU\"",
        "x-parse-application-id": "dev"
      },
      "referrer": "http://localhost:4040/",
      "referrerPolicy": "strict-origin-when-cross-origin",
      "body": null,
      "method": "GET",
      "mode": "cors",
      "credentials": "omit"
    });
    // {"results":[]}
    let data = await result.json();
    let list = data?.results
    return list
  }
}
